﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace RM2012Mobile
{
    public partial class frmServe : Form
    {
        public frmServe()
        {
            InitializeComponent();
        }

        //Send to Server with Message user defined
        private void btnTesting_Click(object sender, EventArgs e)
        {
            Thread tClient = new Thread(new ThreadStart(ClientSend));
            tClient.Start();
        }
        public void ClientSend()
        {
            try
            {
                String txtcontent = "Xin chào";
                MyUdpClient client = new MyUdpClient();
                client.OnConnect(txtcontent);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Client !" + ex.Message);
            }
        }

        //Start Server
        public void StartServer()
        {
            try
            {
                MyUdpServer server = new MyUdpServer();
                server.OnStart();
            }
            catch
            {
                MessageBox.Show("Error Server !");
            }
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            Thread tServer = new Thread(new ThreadStart(StartServer));
            tServer.Start();
        }


        //Send to Server with Message default
        private void btnConnect2_Click(object sender, EventArgs e)
        {
            Thread tClient = new Thread(new ThreadStart(ClientSend2));
            tClient.Start();
        }
        public void ClientSend2()
        {
            try
            {
                MyUdpClient client = new MyUdpClient();
                client.OnConnect();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Client !" + ex.Message);
            }
        }

        //Other
        private void frmServe_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        //ShutDown
        private void btnShutDown_Click(object sender, EventArgs e)
        {
            Thread tClient = new Thread(new ThreadStart(ShutDown));
            tClient.Start();
        }
        public void ShutDown()
        {
            try
            {
                MyUdpClient client = new MyUdpClient();
                client.OnConnect("shutdown");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Client !" + ex.Message);
            }
        }

        //ReStart
        private void btnRestart_Click(object sender, EventArgs e)
        {
            Thread tClient = new Thread(new ThreadStart(ReStart));
            tClient.Start();
        }
        public void ReStart()
        {
            try
            {
                MyUdpClient client = new MyUdpClient();
                client.OnConnect("restart");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Client !" + ex.Message);
            }
        }


        //Log Off
        private void btnLogOff_Click(object sender, EventArgs e)
        {
            Thread tClient = new Thread(new ThreadStart(LogOff));
            tClient.Start();
        }
        public void LogOff()
        {
            try
            {
                MyUdpClient client = new MyUdpClient();
                client.OnConnect("logoff");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Client !" + ex.Message);
            }
        }
    }
}